package com.zby.admin.controller;

import com.zby.admin.pojo.dto.AddAddressDto;
import com.zby.admin.pojo.dto.AddressDto;
import com.zby.admin.pojo.vo.AddressVo;
import com.zby.admin.service.AddressService;
import com.zby.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    @GetMapping("/list")
    public Result<List<AddressVo>> list(HttpServletRequest request) {
        List<AddressVo> addressList = addressService.getAddressListByUserId(request);
        return Result.success(addressList);
    }

    @PostMapping("/add")
    public Result<String> add(@RequestBody AddAddressDto addressDto, HttpServletRequest request) {
        boolean success = addressService.addAddress(addressDto, request);
        if (success) {
            return Result.success("地址添加成功");
        } else {
            return Result.error(401, "地址添加失败，请先登录");
        }
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody AddressDto addressDto, HttpServletRequest request) {
        boolean success = addressService.updateAddress(addressDto, request);
        if (success) {
            return Result.success("地址更新成功");
        } else {
            return Result.error(401, "地址更新失败，地址不存在或无权限");
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id, HttpServletRequest request) {
        boolean success = addressService.deleteAddress(id, request);
        if (success) {
            return Result.success("地址删除成功");
        } else {
            return Result.error(401, "地址删除失败，地址不存在或无权限");
        }
    }
    /**
     * 设置默认地址接口
     *
     * */
    @PutMapping("/setDefault/{id}")
    public Result<String> setDefault(@PathVariable Long id, HttpServletRequest request) {
        boolean success = addressService.setDefault(id, request);
        if (success) {
            return Result.success("设置默认地址成功");
        } else {
            return Result.error(401, "设置默认地址失败，地址不存在或无权限");
        }
    }
}